//This is the main entry point for Windows implementations
//This just starts the runtime to setup the playground for the simulation

#include "kraw.h"
#include "runtime.h"

using namespace Kraw;

//define this in the application so that it can be selected the type of it
extern App* CreateApp();

int main( int argc,  char* argv[] )
{
	Runtime runtime( argc, argv );
	//TODO- define another runtime to handle windows cmd line params
	//TODO- the app settings are specified from command line parameters,
	//or loaded from a configuration file, including:

	//the graphics interface, window size, etc.
	//the project and resource paths or packages
	//the startup scripts
	//and finally open the display
	//and when all of this has been set, the application can be initialized
	App* app = CreateApp();
	app->Init();

	Display* display = Display::Create();
	display->Open();

	Input* input = Input::Get();

	app->pDisplay = display;
	app->pInput = input;

	if (app->Open())
	{
		Time prevTime = 0.0;
		while (display->Update())
		{
			//update application time
			Timer* timer = Timer::Get();
			Time curTime = timer->GetTime();
			if (prevTime == 0.0)
			{
				prevTime = curTime;
			}
			float frameTime = (float) (curTime - prevTime);

			//TODO- tick script server
			//TODO- tick other servers

			input->Tick( curTime );

			//TODO- toggle console

			if (!app->Tick( frameTime ))
				break;

			if (display->BeginDraw())
			{
				app->Draw();
				//display->DrawText();
				display->EndDraw();
			}

			prevTime = curTime;

			input->FlushEvents();

			//sleep(0.0); //multitask friendly
		}
		
		app->Close();
	}

	//close the display
	display->Close();

	//release all objects and servers
    //display->Release();
    input->Release();

	return 1;
}
